import { util } from '../../../../utils/util';
var dateTimePicker = require('../../../../utils/dateTimePicker');
var app = getApp();
//社会实践
Page({
  mixins: [],
  data: {
     activityType:'',    //活动类型
     instanceType:'',       //实例类型
     startYear: util.formatDateTimeYearFive(new Date()),
     endYear: util.formatDateTimeYear(new Date()),
     beginTime:'',
     endTime:'',
     sumTime:0,
     honorFiles:[], //图片数组
     //选择获奖类型
     honorType:'',
      //选择获奖类型
     examines:'',
     gradeArr:[
        {css:'',id:'1',value:'国家级'},{css:'',id:'2',value:'省级'},{css:'',id:'3',value:'市级'},{css:'',id:'4',value:'县级'},{css:'',id:'5',value:'校级'}
    ],
     isQualifiedArr:[
        {css:'',id:'1',value:'合格'},{css:'',id:'2',value:'不合格'}
    ],
    },
    on_grade(e){
      var me = this;
      console.log(e.currentTarget.dataset.index)
      let index=e.currentTarget.dataset.index;
      let arr=me.data.gradeArr;
      let i=0;
      for(;i<arr.length;i++){arr[i].css=''}
      arr[index].css = ' active';
      me.data.honorType=arr[index].id;
      me.setData( me.data);
    },
    isQualified(e){
      var me = this;
      console.log(e.currentTarget.dataset.index)
      let index=e.currentTarget.dataset.index;
      let arr=me.data.isQualifiedArr;
      let i=0;
      for(;i<arr.length;i++){arr[i].css=''}
      arr[index].css = ' active';
      me.data.examines=arr[index].id;
      me.setData( me.data);
    },

    onSubmit(e) {
      var me = this;
      var obj = e.detail.value;
      var dataObj=obj;
      dataObj['honorType'] =me.data.honorType;
      dataObj['examines'] = me.data.examines;
      if(me.data.sumTime<=0){
        util.showError('结束时间必须大于开始时间');
        return;  
      }
      //做非空校验
      if(util.validation(dataObj)){
          obj['sumTime'] = parseFloat(me.data.sumTime);
          obj['honorType'] =parseInt(me.data.honorType);
          obj['examines'] = parseInt(me.data.examines);
          obj['files'] = me.data.honorFiles;
          obj['activityType'] = parseInt(me.data.activityType);
          obj['instanceType'] = parseInt(me.data.instanceType);
          obj['showName'] = 'pracName';
          console.info(obj);
          var postdata = {
               activityType: parseInt(me.data.activityType),
               instanceType: parseInt(me.data.instanceType),
               itemInfo : obj,
               };
           //调用提交方法
           util.submitRecord(postdata);
      } 
    },


    //控制选择器的日跟随月而联动
    changeDateTimeColumn(e) {
      var arr = this.data.dateTime, dateArr = this.data.dateTimeArray;
      arr[e.detail.column] = e.detail.value;
      dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);
      this.setData({ 
        dateTimeArray: dateArr,
        dateTime: arr
      });
    },
  
    stateDatePickerYMDHMS(e) {
      let dateTimeArray=this.data.dateTimeArray;
      let dateTime=e.detail.value;
      let dataValue=dateTimeArray[0][dateTime[0]]+'-'+dateTimeArray[1][dateTime[1]]+'-'+dateTimeArray[2][dateTime[2]]+' '+dateTimeArray[3][dateTime[3]]+':'+dateTimeArray[4][dateTime[4]]
      //选择时间不能大于当前时间
      if(!(util.judgeDateTime(dataValue))){
        return;
      }
      //计算时间差和比较开始时间不能大于结束时间
      var statedate=new Date(dataValue.replace(/-/g, '/')).getTime();  //开始时间  
      var enddate=new Date(this.data.endTime.replace(/-/g, '/')).getTime();  //结束时间 
      if(enddate<=statedate){
        util.showError('结束时间必须大于开始时间');
        return;
      }
     this.setData({
      beginTime:dataValue,
      sumTime:((enddate-statedate)/1000/60/60).toFixed(1)
     })
    },

    endDatePickerYMDHMS(e){
      let dateTimeArray=this.data.dateTimeArray;
      let dateTime=e.detail.value;
      let dataValue=dateTimeArray[0][dateTime[0]]+'-'+dateTimeArray[1][dateTime[1]]+'-'+dateTimeArray[2][dateTime[2]]+' '+dateTimeArray[3][dateTime[3]]+':'+dateTimeArray[4][dateTime[4]]
      //选择时间不能大于当前时间
      if(!(util.judgeDateTime(dataValue))){
        return;
      }
      //计算时间差和比较开始时间不能大于结束时间
      var statedate=new Date(this.data.beginTime.replace(/-/g, '/')).getTime();  //开始时间  
      var enddate=new Date(dataValue.replace(/-/g, '/')).getTime();  //结束时间 
      if(enddate<=statedate){
        util.showError('结束时间必须大于开始时间');
        return;
      }
     this.setData({
      endTime:dataValue,
      sumTime:((enddate-statedate)/1000/60/60).toFixed(1)
     })
    },
  
  onLoad(query) {
    // 页面加载
    console.info(`Page onLoad with query: ${JSON.stringify(query)}`);
    // 获取完整的年月日 时分秒，以及默认显示的数组
    var obj = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
    // 精确到分的处理，将数组的秒去掉
    obj.dateTimeArray.pop();
    this.setData({
      dateTimeArray: obj.dateTimeArray,
      dateTime1: obj.dateTime,
      dateTime2: obj.dateTime,
      dateTime: obj.dateTime
    });


    var arr = query.honorFiles.split(",");
    this.setData({
      activityType: query.activityType,
      instanceType: query.instanceType,
      honorFiles: arr,
        //选择获奖类型
      honorType:'',
      //选择获奖类型
      examines:'',
      gradeArr:[
          {css:'',id:'1',value:'国家级'},{css:'',id:'2',value:'省级'},{css:'',id:'3',value:'市级'},{css:'',id:'4',value:'县级'},{css:'',id:'5',value:'校级'}
      ],
      isQualifiedArr:[
          {css:'',id:'1',value:'合格'},{css:'',id:'2',value:'不合格'}
      ]
    });
  },
  onUnload() {
    // 页面被关闭
    wx.setStorageSync("honorFiles", []);
  },
});
